import { getToken, removeToken } from '@/utils/storage'
import { loginApi } from '@/api/user'
export default {
    state() {
        return {
            token: getToken(),
            query: '',
        }
    },
    mutations: {
        logout(state) {
            state.token = ''
            state.username = ''
            removeToken()
        },
        setInfo(state, data) {
            state.token = data.token;
            state.username = data.query;
        },
        setPath(state, path) {
            state.path = path
        }
    },
    actions: {
        async login({ commit }, data) {
            const res = await loginApi(data)
            if (res.data.token) commit('setInfo', res.data)
            return res.data
        }
    },
    namespaced: true
}